<template>
  <div class="flowpath-container">
    <div>
      <div class="mb20">
        <span class="color_494949 ft12 pr5 w70">{{ translate('PDA标识号') }}：</span>
        <el-input
          v-model="searchInfo"
          class="w164 mr10"
          :placeholder="translate('请输入关键字')"
        />
        <span class="color_494949 ft12 pr5 w70">{{ translate('PDA名称') }}：</span>
        <el-input
          v-model="searchInfo3"
          class="w164 mr10"
          :placeholder="translate('请输入关键字')"
        />
        <span class="color_494949 ft12 pr5 w70 ml10">{{ translate('状态') }}：</span>
        <el-select
          v-model="PickingName"
          class="w164"
          :placeholder="translate('请选择')"
         >
          <el-option
            v-for="item in PickingNameArry"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
          </el-select>
        <el-button class="settingBtn ml10" type="primary" @click="getList">
          {{ translate('查询') }}
        </el-button>
      </div>
      <div class="add">
        <el-button
          v-if="MenuAuths('add')"
          class="settingBtn"
          type="primary"
          @click="addFn"
          plain
        >
          {{ translate('新增') }}
        </el-button>
      </div>
      <el-table
          :data="tableData"
          :loading="loading"
          :row-class-name="tableRowClassName"
          :row-key="getRowKey"
          @selection-change="handleSelectionChange"
          >
        <el-table-column prop="value" type="selection" width="55" />
        <!-- <el-table-column :label="translate('领料单编码')" prop="PickingCode" /> -->
        <el-table-column :label="translate('PDA标识号')">
          <template #default="{ row }">
            <div>
              <span class="iconcolor">{{ row.PDAID }}</span>
              <vab-icon icon="file-copy-line" @click="copyToClipboard(row.PDAID)" />
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="translate('PDA名称')" prop="PDAName" />
        <el-table-column :label="translate('校验码')" prop="Checkcode" />
        <el-table-column :label="translate('状态')" min-width="150">
          <template #default="{ row }">
            <div>
              <el-tag type="success">{{ row.Status }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="translate('有效时间')"
          prop="Enabled"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('备注')"
          prop="Remark"
          show-overflow-tooltip
        />
        <el-table-column :label="translate('操作')" width="300">
          <template #default="{ row }">
            <el-button
              v-if="MenuAuths('edit')"
              plain
              size="small"
              type="primary"
              @click="detailFn(row)"
            >
              {{ translate('编辑') }}
            </el-button>
            <el-button
              v-if="MenuAuths('delete')"
              plain
              size="small"
              type="danger"
              @click="deletedFn(row)"
            >
              {{ translate('删除') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          :current-page="pageNo"
          :layout="layout"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      :title="translate('PDA')"
      width="400px"
      @close="closeFn"
      :close-on-click-modal="false"
    >
      <detail
        :chileddetail="chileddetail"
        :form="form"
      />
      <template #footer>
        <el-button @click="dialogVisible = false">
          {{ translate('取消') }}
        </el-button>
        <el-button :disabled="isclick" type="primary" @click="submit">
          {{ translate('确定') }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    PDAdelpda,
    pickingbillUpdateData,
    PDAadd,
  } from '@/api/form'
  import { PDAGetPageData } from '@/api/mupro'
  import detail from './components/detail'
  import { MenuAuth } from '~/src/utils/permission'
  import VabPrint from '@/plugins/VabPrint'
  import { translate } from '@/i18n'
  import { parseTime } from '@/utils'
  export default defineComponent({
    name: 'Flowpath',
    components: { detail },
    data() {
      return {
        tableData: [],
        searchInfo: '',
        searchInfo3: '',
        wheres: '',
        total: 0,
        pageNo: 1,
        pageSize: 20,
        dialogVisible: false,
        enabled: 0,
        DetailPageData: {},
        chileddetail: [],
        chiledPrintdetail: [],
        isclick: false,
        Remark: '全部',
        PickingName: '全部',
        PickingNameArry: [
          {
            label: '全部',
            value: '全部',
          },
          {
            label: '正常',
            value: '正常',
          },
          {
            label: '作废',
            value: '作废',
          },
        ],
        submitType: 'add',
        layout: 'total, sizes, prev, pager, next, jumper',
        deleList: [],
        MenuAuths: '',
        tableRef: null,
        isPrint: false,
        loading: false,
        status: -1,
        PBMID: '',
        selectArr: [],
        form: {
          PDAID: '',
          PDAName: '',
        },
        rules: {
          code: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入工号'),
            },
          ],
          Account: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入登录账号'),
            },
          ],
          workFlowName: [
            {
              required: true,
              trigger: 'blur',
              message: translate('请输入账号名称'),
            },
          ],
        },
      }
    },
    created() {
      this.MenuAuths = MenuAuth
      this.getList()
    },
    methods: {
      tableRowClassName({ row, rowIndex }) {
        row.row_index = rowIndex
      },
      handleSelectionChange(e) {
        this.selectArr = e
      },
      getRowKey(row) {
        return row.PBMID
      },
      //复制标识
      async copyToClipboard(val) {
        if (navigator.clipboard && window.isSecureContext) {
          try {
          console.log(navigator.clipboard)
          console.log(window.isSecureContext)
          await navigator.clipboard.writeText(val)
          this.$message({
            message:'复制成功',
            type:'success',
          })
          } catch (err) {
            this.$message({
            message:'复制失败',
            type:'error',
          })
          }

        }else {
          // 创建text area
        const textArea = document.createElement("textarea")
        textArea.value = val
        // 使text area不在viewport，同时设置不可见
        textArea.style.position = "absolute"
        textArea.style.opacity = 0
        textArea.style.left = "-999999px"
        textArea.style.top = "-999999px"
        document.body.appendChild(textArea)
        textArea.focus()
        textArea.select()

        return new Promise((res, rej) => {
          // 执行复制命令并移除文本框
          document.execCommand("copy") ? res() : rej()
          textArea.remove()
        }).then(() => {
          this.$message({
              message: "复制成功",
              type: "success",
            })
          }).catch((err) => {
            this.$message({
              message: "复制失败",
              type: "error",
            })
          })
        }

      },
      translate(row) {
        return translate(row)
      },
      async getList() {
        this.wheres = ''
        if (this.PickingName || this.searchInfo) {
          const arr = []
          this.searchInfo.trim() !== '' &&
            arr.push({
              name: 'PickingUnit',
              value: this.searchInfo,
              displayType: 'like',
            })
          this.PickingName.trim() !== '' &&
            arr.push({
              name: 'BillStatus',
              value: this.PickingName === '全部' ? '' : this.PickingName,
              displayType: 'like',
            })
          this.wheres = JSON.stringify(arr)
        }
        sessionStorage.setItem('PBRemark', this.Remark)
        sessionStorage.setItem('PBsearchInfo3', this.searchInfo3)

        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: '',
          export: false,
          value: '',
          openid: ''
        }
        const data = await PDAGetPageData(params)
        this.tableData = data.data.rows
        this.total = data.data.total
        console.log(this.tableData)
      },
      async deletedFn(row) {
        this.$confirm(translate('确认删除该文件吗?'), translate('提示'), {
          confirmButtonText: translate('确定'),
          cancelButtonText: translate('取消'),
          type: 'warning',
        })
          .then(() => {
            this.deleted(row)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: translate('已取消删除'),
            })
          })
      },
      async deleted(row) {
        const params = {
          id: row.ID
        }
        const data = await PDAdelpda(params)
        if (data.code == '200') {
          this.$message({
            message: translate('删除成功'),
            type: 'success',
          })
          await this.getList()
        }
      },
      //分页条数切换
      handleSizeChange(val) {
        this.pageSize = val
        this.getList()
      },
      //分页切换
      handleCurrentChange(val) {
        this.pageNo = val
        this.getList()
      },
      async addFn() {
        this.submitType = 'add'
        this.status = -1
        this.form = {
          PDAID: '',
          PDAName: '',
        }
        this.chileddetail = []
        this.dialogVisible = true
        this.deleList = []
      },
      submit() {
        if (this.submitType === 'add') {
          const randomNumber = this.generateRandomFourDigitNumber()
          console.log(randomNumber)
          this.AddData(randomNumber)
        } else {
          this.updataSaveList()
        }
      },

      generateRandomFourDigitNumber() {
        let num;
        do {
          num = Math.floor(1000 + Math.random() * 9000);
        } while (num < 1000 || num > 9999);
        return num
      },
      closeFn() {
        this.enabled = 0
      },
      async detailFn(row) {
        this.form = row
        this.PBMID = row.ID
        this.submitType = 'edit'
        this.dialogVisible = true
      },
      async AddData(num) {
        this.isclick = true
        const params = {
          pdaid: this.form.PDAID,
          pdaName: this.form.PDAName,
          checkcode: num
        }
        const data = await PDAadd(params)
        if (data.code == '200') {
          this.getList()
          this.$message({
            message: translate('保存成功'),
            type: 'success',
          })
          this.dialogVisible = false
        }
        setTimeout(() => {
          this.isclick = false
        }, 5000)
      },
      async updataSaveList() {
        const params = this.form
        const data = await PDAadd(params)
        if (data.code == '200') {
          this.getList()
          this.$message({
            message: translate('保存成功'),
            type: 'success',
          })
          this.dialogVisible = false
        }
      },
    },
  })
</script>
<style lang="scss" scoped>
  .flowpath-container {
    .color_494949 {
      color: #494949;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }
    .mr10 {
      margin-right: 10px;
    }
    .ml10 {
      margin-left: 10px;
    }
    .add {
      margin-bottom: 10px;
      text-align: left;
    }

    .iconcommon {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: blue;
    }
    .iconcommon2 {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: red;
    }
    .w70 {
      display: inline-block;
      width: 90px;
      text-align: right;
    }
    .condition-container {
      display: inline-block;
      margin-top: 10px;
    }
    .iconcolor {
      color: #409eff;
    }
    .color_l {
      color: #67c23a;
    }
    .color_red {
      color: #f56c6c;
    }
  }
</style>
